package com.jic.member.controller.rest;

import com.google.common.base.Splitter;
import com.jic.common.base.vo.RestResult;
import com.jic.member.api.rest.MememberApi;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.MememberUser;
import com.jic.member.request.MememberInfoRequest;
import com.jic.member.request.MememberUserForMarketRequest;
import com.jic.member.request.MememberUserRequest;
import com.jic.member.response.MemberInfoResponse;
import com.jic.member.response.MememberUserResponse;
import com.jic.member.service.MememberUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/user")
@Api(tags = {"会员接口"})
@Slf4j
public class MememberOperationController implements MememberApi {

    @Autowired
    private MememberUserService mememberUserService;

    @Override
    @ApiOperation(value = "新增会员信息接口")
    @ResponseBody
    @RequestMapping(value = "/saveMemberUserInfo")
    public RestResult saveMemberUserInfo(@RequestBody MememberUserRequest form) {
        RestResult restResult = new RestResult();
        MememberUser mememberUser = BeanUtil.copy(form, MememberUser.class);
        long flag = mememberUserService.insert(mememberUser);
        if(flag > 0) {
            restResult.setCode("0000");
            restResult.setData(flag);
            restResult.setMsg("调用成功");
        }else{
            restResult.setData(flag);
            restResult.setMsg("新增记录失败");
        }
        return restResult;
    }

    @Override
    @ApiOperation(value = "查询会员信息接口")
    @PostMapping(value = "/queryMemberUserInfo")
    public RestResult<MememberUserResponse> queryMemberUserInfo(@RequestBody Long id) {
        MememberUser mememberUser = mememberUserService.queryByPrimaryKey(id);
        MememberUserResponse mememberUserResponse=new MememberUserResponse();
        if(null!=mememberUser){
            BeanUtils.copyProperties(mememberUser,mememberUserResponse);
        }
        return RestResult.success(mememberUserResponse);
    }



    /**
     * 查询会员列表
     * 需要注意的是【mem_member_user】表中的dept_id是无效的；所以关联的【mem_member_action】表来锁定平台Id；
     * @param request 请求入参
     * @return 返回封装的实体来
     * @author tzc
     * @date 2020-7-10 19:31:51
     */
    @Override
    @ApiOperation(value = "查询会员信息接口")
    @ResponseBody
    @RequestMapping(value = "/listMemberUserInfo")
    public RestResult listMemberUserInfo(@RequestBody MememberUserRequest request) {
        MememberUser user = new MememberUser();
        BeanUtils.copyProperties(request,user);
        if (null != request && null != request.getDeptIds()){
            List<String> deptIds = Splitter.on(",").splitToList(request.getDeptIds());
            user.setDeptIds(deptIds);
        }

        return RestResult.success(mememberUserService.listMemberUserInfoInDeptIds(user));
    }

    @Override
    @ApiOperation(value = "修改会员信息接口")
    @ResponseBody
    @RequestMapping(value = "/updateMemberUserInfo")
    public RestResult updateMemberUserInfo(@RequestBody MememberUserRequest form) {
        RestResult restResult = new RestResult();
        log.info("修改信息=========》,{}", form.toString());
        MememberUser mememberUser = BeanUtil.copy(form, MememberUser.class);
        long val = mememberUserService.updateMemberUserInfo(mememberUser);
        if(val > 0) {
            restResult.setCode("0000");
            restResult.setData(mememberUser);
            restResult.setMsg("调用成功");
        }else{
            restResult.setMsg("修改记录失败");
        }
        return restResult;
    }

    @Override
    @ApiOperation(value = "更新会员信息接口")
    @ResponseBody
    @RequestMapping(value = "/batchUpdateMemberUser")
    public RestResult batchUpdateMemberUser(@RequestBody MememberUserRequest form) {
        RestResult restResult = new RestResult();
        log.info("修改信息=========》,{}", form.toString());
        MememberUser mememberUser = BeanUtil.copy(form, MememberUser.class);
        long val = mememberUserService.batchUpdateMemberUser(mememberUser);
        if(val > 0) {
            restResult.setCode("0000");
            restResult.setData(mememberUser);
            restResult.setMsg("调用成功");
        }else{
            restResult.setMsg("修改记录失败");
        }
        return restResult;
    }


    /**
     * 为活动中心提供会员列表接口
     * @param request 请求参数
     * @return 响应参数
     * @author tzc
     * @date 2020-7-2 22:07:45
     */
    @Override
    @ApiOperation(value = "查询会员信息接口")
    @ResponseBody
    @RequestMapping(value = "/listMemberUserInfoForMarket")
    public RestResult listMemberUserInfoForMarket(@RequestBody MememberUserForMarketRequest request) {
        return mememberUserService.listMemberUserInfoForMarket(request);
    }


    @ApiOperation(value = "聚合用户信息及微常准用户信息", notes = "聚合用户信息及微常准用户信息")
    @PostMapping("/getAggregationUserInfo")
    public RestResult<MemberInfoResponse> getAggregationUserInfo(@Valid @RequestBody MememberInfoRequest request) {
        try {
            return mememberUserService.aggregationUserInfo(request);
        }catch (Exception e){
            log.info(e.getMessage());
            return RestResult.error("9999","获取用户信息失败");
        }
    }
}
